<?php

/**
 * Framework_Presenter_Module
 *
 * @author      Joe Stump <joe@joestump.net>
 * @copyright   (c) 2006 Joseph C. Stump. All rights reserved.
 * @license http://www.opensource.org/licenses/bsd-license.php
 * @package Framework
 * @filesource
 */

require_once 'HTML/Template/Smarty/Smarty.class.php';

/**
 * Framework_Presenter_Module
 *
 * With the default Smarty presenter your module's template is wrapped in the
 * page.tpl template. With the Module presenter your module's template is NOT
 * wrapped in the page.tpl template.
 *
 * @author Joe Stump <joe@joestump.net>
 * @package Framework
 * @subpackage Presenter
 * @link http://smarty.php.net
 */
class Framework_Presenter_Module extends Framework_Presenter_Common
{
	/**
	 * $template
	 *
	 * @access private
	 * @var object $template Instance of Smarty
	 */
	private $template = null;
	
	/**
	 * $cacheInit
	 *
	 * @access 		protected
	 * @var 		array 		$cacheInit		Whether or not caching is initialized
	 */
	protected $cacheInit = false;
	
	/**
     * cacheInit
     *
     * Initialized caching
     *
     * @access      public
     * @return      boolean
     */
	public function cacheInit()
	{

		$this->template->caching = 1;
		$this->template->cache_lifetime = 7200;
		$this->template->compile_check = true;
			
		if($this->template->is_cached($this->module->templateCache['template'],Framework::$request->ownerName.'|'.$this->module->moduleName.'|'.$this->module->templateCache['template'].'|'.$this->module->templateCache['id'])) {
			$this->cacheInit = true;
			return true;
		} else {
			return false;
		}




	}
	
	/**
     * clearCache
     *
     * Clears the template cache
     *
     * @access      public
     * @return      void
     */
	public function clearCache()
	{
		$this->template->clear_cache(null, Framework::$request->ownerName.'|'.$this->module->moduleName.'|'.$this->module->templateCache['template']);
	}
	

	/**
	 * __construct
	 *
	 * @access public
	 * @param object $module Instance of Framework_Module to be displayed
	 * @return void
	 */
	public function __construct(Framework_Module $module)
	{
		parent::__construct($module);
		
		// Check for custom templat/skin
		$owner = Framework_Owner::singleton();
		
		if ($owner->isValid && $owner->skin) {
			$skin = $owner->skin;
		} else {
			$skin = 'Default';
		}
		
		$this->template = Framework_Template::factory('Smarty', $module->moduleName, $skin);
	}

	/**
	 * display
	 *
	 * The Framework_Presenter_Module class only renders the module's template
	 * file and not the page template that the Framework_Presenter_Smarty
	 * presenter renders as well.
	 *
	 * @access public
	 * @return void
	 */

	public function display()
	{

		$this->template->assign('modulePath',$path);
		$this->template->assign('site',Framework::$site);
		$this->template->assign('tplFile',$tplFile);
		$this->template->assign('user',$this->user);
		$this->template->assign('session',$this->session);
		$this->template->assign('filePath',Framework::$request->getBaseHTTPPath(false));
		$this->template->assign('uriPath',Framework::$request->getBaseHTTPPath());
		$this->template->assign('request',Framework::$request);

		if ($this->module->ran) {
			// Check to see if module has form objects
			// if so, render them to array and assign them
			if (count($this->module->forms)){
				require_once "HTML/QuickForm/Renderer/ArraySmarty.php";
				$renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->template);
				foreach ($this->module->forms as $var => $val){
					$val->accept($renderer);
					$this->template->assign($var,$renderer->toArray());
				}
			}
	
			foreach ($this->module->getData() as $var => $val) {
				if (!in_array($var,array('path','tplFile'))) {
					$this->template->assign($var,$val);
				}
			}
		}

		//$this->template->display($path . '/'. $this->module->tplFile);

		if (!is_null($this->module->templateCache)) {
//			if ($this->module->ran) {
//				echo 'ran';
//			}
			$this->template->display($this->module->templateCache['template'], Framework::$request->ownerName.'|'.$this->module->moduleName.'|'.$this->module->templateCache['template'].'|'.$this->module->templateCache['id']);
		} else {
			$this->template->display($this->module->tplFile);
		}
	}
}



		

?>
